﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类WorkReport_InBox。
    /// </summary>
    public class WorkReport_InBox
    {
        public WorkReport_InBox()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from WorkReport_InBox where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.WorkReport_InBox model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into WorkReport_InBox(");
            strSql.Append("RecordId,WorkReportId,ReceiverId,ReceiverName,ReceiverDepartId,ReceiverDepartName,ReceiverCompanyId,ReceiverCompanyName,ReceiverCityId,ReceiverCityName,isReaded,isDeleted)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@WorkReportId,@ReceiverId,@ReceiverName,@ReceiverDepartId,@ReceiverDepartName,@ReceiverCompanyId,@ReceiverCompanyName,@ReceiverCityId,@ReceiverCityName,@isReaded,@isDeleted)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "WorkReportId", DbType.Guid, model.WorkReportId);
            db.AddInParameter(dbCommand, "ReceiverId", DbType.Guid, model.ReceiverId);
            db.AddInParameter(dbCommand, "ReceiverName", DbType.AnsiString, model.ReceiverName);
            db.AddInParameter(dbCommand, "ReceiverDepartId", DbType.Guid, model.ReceiverDepartId);
            db.AddInParameter(dbCommand, "ReceiverDepartName", DbType.AnsiString, model.ReceiverDepartName);
            db.AddInParameter(dbCommand, "ReceiverCompanyId", DbType.Guid, model.ReceiverCompanyId);
            db.AddInParameter(dbCommand, "ReceiverCompanyName", DbType.AnsiString, model.ReceiverCompanyName);
            db.AddInParameter(dbCommand, "ReceiverCityId", DbType.Guid, model.ReceiverCityId);
            db.AddInParameter(dbCommand, "ReceiverCityName", DbType.AnsiString, model.ReceiverCityName);
            db.AddInParameter(dbCommand, "isReaded", DbType.Boolean, model.isReaded);
            db.AddInParameter(dbCommand, "isDeleted", DbType.Boolean, model.isDeleted);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.WorkReport_InBox model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update WorkReport_InBox set ");
            strSql.Append("WorkReportId=@WorkReportId,");
            strSql.Append("ReceiverId=@ReceiverId,");
            strSql.Append("ReceiverName=@ReceiverName,");
            strSql.Append("ReceiverDepartId=@ReceiverDepartId,");
            strSql.Append("ReceiverDepartName=@ReceiverDepartName,");
            strSql.Append("ReceiverCompanyId=@ReceiverCompanyId,");
            strSql.Append("ReceiverCompanyName=@ReceiverCompanyName,");
            strSql.Append("ReceiverCityId=@ReceiverCityId,");
            strSql.Append("ReceiverCityName=@ReceiverCityName,");
            strSql.Append("isReaded=@isReaded,");
            strSql.Append("isDeleted=@isDeleted");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "WorkReportId", DbType.Guid, model.WorkReportId);
            db.AddInParameter(dbCommand, "ReceiverId", DbType.Guid, model.ReceiverId);
            db.AddInParameter(dbCommand, "ReceiverName", DbType.AnsiString, model.ReceiverName);
            db.AddInParameter(dbCommand, "ReceiverDepartId", DbType.Guid, model.ReceiverDepartId);
            db.AddInParameter(dbCommand, "ReceiverDepartName", DbType.AnsiString, model.ReceiverDepartName);
            db.AddInParameter(dbCommand, "ReceiverCompanyId", DbType.Guid, model.ReceiverCompanyId);
            db.AddInParameter(dbCommand, "ReceiverCompanyName", DbType.AnsiString, model.ReceiverCompanyName);
            db.AddInParameter(dbCommand, "ReceiverCityId", DbType.Guid, model.ReceiverCityId);
            db.AddInParameter(dbCommand, "ReceiverCityName", DbType.AnsiString, model.ReceiverCityName);
            db.AddInParameter(dbCommand, "isReaded", DbType.Boolean, model.isReaded);
            db.AddInParameter(dbCommand, "isDeleted", DbType.Boolean, model.isDeleted);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WorkReport_InBox ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void DeleteByReportId(Guid ReportId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WorkReport_InBox ");
            strSql.Append(" where WorkReportId=@ReportId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ReportId", DbType.Guid, ReportId);
            db.ExecuteNonQuery(dbCommand);

        }
        
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.WorkReport_InBox GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,WorkReportId,ReceiverId,ReceiverName,ReceiverDepartId,ReceiverDepartName,ReceiverCompanyId,ReceiverCompanyName,ReceiverCityId,ReceiverCityName,isReaded,isDeleted from WorkReport_InBox ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.WorkReport_InBox model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,WorkReportId,ReceiverId,ReceiverName,ReceiverDepartId,ReceiverDepartName,ReceiverCompanyId,ReceiverCompanyName,ReceiverCityId,ReceiverCityName,isReaded,isDeleted ");
            strSql.Append(" FROM WorkReport_InBox ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize, int PageIndex, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "WorkReport_InBox");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.WorkReport_InBox> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,WorkReportId,ReceiverId,ReceiverName,ReceiverDepartId,ReceiverDepartName,ReceiverCompanyId,ReceiverCompanyName,ReceiverCityId,ReceiverCityName,isReaded,isDeleted ");
            strSql.Append(" FROM WorkReport_InBox ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.WorkReport_InBox> list = new List<Chenxin.Lib.Hr.Entity.WorkReport_InBox>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.WorkReport_InBox ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.WorkReport_InBox model = new Chenxin.Lib.Hr.Entity.WorkReport_InBox();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["WorkReportId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.WorkReportId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ReceiverId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiverId = new Guid(ojb.ToString());
            }
            model.ReceiverName = dataReader["ReceiverName"].ToString();
            ojb = dataReader["ReceiverDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiverDepartId = new Guid(ojb.ToString());
            }
            model.ReceiverDepartName = dataReader["ReceiverDepartName"].ToString();
            ojb = dataReader["ReceiverCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiverCompanyId = new Guid(ojb.ToString());
            }
            model.ReceiverCompanyName = dataReader["ReceiverCompanyName"].ToString();
            ojb = dataReader["ReceiverCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiverCityId = new Guid(ojb.ToString());
            }
            model.ReceiverCityName = dataReader["ReceiverCityName"].ToString();
            ojb = dataReader["isReaded"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.isReaded = (bool)ojb;
            }
            ojb = dataReader["isDeleted"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.isDeleted = (bool)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

